/**
 * Copyright (c) 2008, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.propertydescriptor.definitions.renderstate;

import com.acarter.propertytable.Property;
import com.acarter.propertytable.PropertySection;
import com.acarter.propertytable.PropertySectionState;
import com.acarter.propertytable.propertyobject.BooleanPropertyObject;
import com.acarter.propertytable.propertyobject.EnumPropertyObject;
import com.acarter.propertytable.propertyobject.IntegerPropertyObject;
import com.acarter.propertytable.propertyobject.BooleanPropertyObject.I_BooleanPropertyObjectListener;
import com.acarter.propertytable.propertyobject.EnumPropertyObject.I_EnumPropertyObjectListener;
import com.acarter.propertytable.propertyobject.IntegerPropertyObject.I_IntegerPropertyObjectListener;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.StencilState;
import com.jme.scene.state.StencilState.StencilFunction;
import com.jme.scene.state.StencilState.StencilOperation;

/**
 * @author Carter
 *
 */
public class StencilStatePropertyPage extends RenderStatePropertyPage {
	
	/**
	 * 
	 */
	public StencilStatePropertyPage() {
		
		PropertySection section = new PropertySection("Stencil State");
		section.addProperty(new Property("Use Two Sided", new BooleanPropertyObject()));
		section.addProperty(new Property("Function Front", new EnumPropertyObject<StencilFunction>()));
		section.addProperty(new Property("Function Back", new EnumPropertyObject<StencilFunction>()));
		section.addProperty(new Property("Fail Front", new EnumPropertyObject<StencilOperation>()));
		section.addProperty(new Property("Fail Back", new EnumPropertyObject<StencilOperation>()));
		section.addProperty(new Property("Z Fail front", new EnumPropertyObject<StencilOperation>()));
		section.addProperty(new Property("Z Fail Back", new EnumPropertyObject<StencilOperation>()));
		section.addProperty(new Property("Z Pass front", new EnumPropertyObject<StencilOperation>()));
		section.addProperty(new Property("Z Pass Back", new EnumPropertyObject<StencilOperation>()));
		section.addProperty(new Property("Reference Front", new IntegerPropertyObject()));
		section.addProperty(new Property("Reference Back", new IntegerPropertyObject()));

		section.setState(PropertySectionState.EXPANDED);
		
		model.addPropertySection(0, section);
	}
	
	/**
	 * 
	 * @param table
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void updateListeners(final RenderState renderstate) {
		
		super.updateListeners(renderstate);
		
		if(!(renderstate instanceof StencilState))
			return;
		
		final StencilState state = (StencilState)renderstate;
		
		BooleanPropertyObject twoSided = (BooleanPropertyObject)model.getPropertySection("Stencil State").getProperty("Use Two Sided").getPropertyObject();
		
		twoSided.SetListener(new I_BooleanPropertyObjectListener() {

			public boolean readValue() {
				
				return state.isUseTwoSided();
			}

			public void saveValue(boolean value) {
				
				state.setUseTwoSided(value);
			}
		});
		
		EnumPropertyObject<StencilFunction> funcFront = (EnumPropertyObject<StencilFunction>)model.getPropertySection("Stencil State").getProperty("Function Front").getPropertyObject();
		
		funcFront.SetListener(new I_EnumPropertyObjectListener<StencilFunction>() {

			public StencilFunction readValue() {
				
				return state.getStencilFunctionFront();
			}

			public void saveValue(StencilFunction value) {
				
				state.setStencilFunctionFront(value);
			}
		});
		
		EnumPropertyObject<StencilFunction> funcBack = (EnumPropertyObject<StencilFunction>)model.getPropertySection("Stencil State").getProperty("Function Back").getPropertyObject();
		
		funcBack.SetListener(new I_EnumPropertyObjectListener<StencilFunction>() {

			public StencilFunction readValue() {
				
				return state.getStencilFunctionBack();
			}

			public void saveValue(StencilFunction value) {
				
				state.setStencilFunctionBack(value);
			}
		});
		
		EnumPropertyObject<StencilOperation> failFront = (EnumPropertyObject<StencilOperation>)model.getPropertySection("Stencil State").getProperty("Fail Front").getPropertyObject();
		
		failFront.SetListener(new I_EnumPropertyObjectListener<StencilOperation>() {

			public StencilOperation readValue() {
				
				return state.getStencilOpFailFront();
			}

			public void saveValue(StencilOperation value) {
				
				state.setStencilOpFailFront(value);
			}
		});
		
		EnumPropertyObject<StencilOperation> failBack = (EnumPropertyObject<StencilOperation>)model.getPropertySection("Stencil State").getProperty("Fail Back").getPropertyObject();
		
		failBack.SetListener(new I_EnumPropertyObjectListener<StencilOperation>() {

			public StencilOperation readValue() {
				
				return state.getStencilOpFailBack();
			}

			public void saveValue(StencilOperation value) {
				
				state.setStencilOpFailBack(value);
			}
		});
		
		EnumPropertyObject<StencilOperation> zFailFront = (EnumPropertyObject<StencilOperation>)model.getPropertySection("Stencil State").getProperty("Z Fail Front").getPropertyObject();
		
		zFailFront.SetListener(new I_EnumPropertyObjectListener<StencilOperation>() {
			public StencilOperation readValue() {
				
				return state.getStencilOpZFailFront();
			}

			public void saveValue(StencilOperation value) {
				
				state.setStencilOpZFailFront(value);
			}
		});
		
		EnumPropertyObject<StencilOperation> zFailBack = (EnumPropertyObject<StencilOperation>)model.getPropertySection("Stencil State").getProperty("Z Fail Back").getPropertyObject();
		
		zFailBack.SetListener(new I_EnumPropertyObjectListener<StencilOperation>() {

			public StencilOperation readValue() {
				
				return state.getStencilOpZFailBack();
			}

			public void saveValue(StencilOperation value) {
				
				state.setStencilOpZFailBack(value);
			}
		});
		
		EnumPropertyObject<StencilOperation> zPassFront = (EnumPropertyObject<StencilOperation>)model.getPropertySection("Stencil State").getProperty("Z Pass Front").getPropertyObject();
		
		zPassFront.SetListener(new I_EnumPropertyObjectListener<StencilOperation>() {

			public StencilOperation readValue() {
				
				return state.getStencilOpZPassFront();
			}

			public void saveValue(StencilOperation value) {
				
				state.setStencilOpZPassFront(value);
			}
		});
		
		EnumPropertyObject<StencilOperation> zPassBack = (EnumPropertyObject<StencilOperation>)model.getPropertySection("Stencil State").getProperty("Z Pass Back").getPropertyObject();
		
		zPassBack.SetListener(new I_EnumPropertyObjectListener<StencilOperation>() {

			public StencilOperation readValue() {
				
				return state.getStencilOpZPassBack();
			}

			public void saveValue(StencilOperation value) {
				
				state.setStencilOpZPassBack(value);
			}
		});
		
		IntegerPropertyObject refFront = (IntegerPropertyObject)model.getPropertySection("Stencil State").getProperty("Reference Front").getPropertyObject();
		
		refFront.SetListener(new I_IntegerPropertyObjectListener() {

			public int readValue() {
				
				return state.getStencilReferenceFront();
			}

			public void saveValue(int value) {
				
				state.setStencilReferenceFront(value);
			}
		});
		
		IntegerPropertyObject refBack = (IntegerPropertyObject)model.getPropertySection("Stencil State").getProperty("Reference Back").getPropertyObject();
		
		refBack.SetListener(new I_IntegerPropertyObjectListener() {

			public int readValue() {
				
				return state.getStencilReferenceBack();
			}

			public void saveValue(int value) {
				
				state.setStencilReferenceBack(value);
			}
		});
	}

}
